﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Minecraft_2D.Blocks;

namespace Minecraft_2D
{
    public partial class Form1 : Form
    {
        #region Attributs
        private Panel[,] _panelMap;
        private int[] _listBlocs;
        private int _selectedBloc;
        private int _length, _height, tableLength, tableHeight;
        private Map _map;
        private Point _player;
        public Point arbre;
        #endregion

        #region Constructeur
        public Form1()
        {
            InitializeComponent();
            this.MouseWheel += Form1_MouseWheel;

            _length = 50;
            _height = 50;
            tableLength = tableLayoutPanel1.ColumnCount;
            tableHeight = tableLayoutPanel1.RowCount;
            _listBlocs = new int[tableLayoutPanel2.ColumnCount];
            _selectedBloc = 0;
            _map = new Map(_length, _height, this);
            _player = new Point(0, 0);

            _panelMap = new Panel[tableLength, tableHeight];

            for (int i = 0; i < tableLength; ++i)
            {
                for (int j = 0; j < tableHeight; ++j)
                {
                    int intBlock = _map[i, j];
                    Bloc b = BlocSet.GetSingleton()[intBlock];

                    Panel p = new Panel();
                    p.Dock = DockStyle.Fill;
                    p.BackgroundImage = b.Image;
                    p.Margin = new System.Windows.Forms.Padding(0);
                    p.BackgroundImageLayout = ImageLayout.Stretch;
                    p.MouseClick += p_MouseClick;

                    _panelMap[i, j] = p;
                }
            }
            for (int i = 1; i <= tableLayoutPanel2.ColumnCount; i++)
            {
                Bloc b = BlocSet.GetSingleton()[i];

                Panel p = new Panel();
                p.Dock = DockStyle.Fill;
                p.BackgroundImage = b.Image;
                p.Margin = new System.Windows.Forms.Padding(0);
                p.BackgroundImageLayout = ImageLayout.Stretch;
                p.Tag = b;
                tableLayoutPanel2.Controls.Add(p, i - 1, 0);
                _listBlocs[i - 1] = i;
            }
            for (int i = 0; i < tableLength; ++i)
            {
                for (int j = 0; j < tableHeight; ++j)
                {
                    tableLayoutPanel1.Controls.Add(_panelMap[i, j], i, j);
                    _panelMap[i, j].Tag = new Point(i, j);
                }
            }
            //Build();
        }
        #endregion

        #region Proprietes
        public Map Map
        {
            get { return _map; }
            set { _map = value; }
        }
        #endregion

        #region Methodes
        public void Build()
        {
            int left = _player.X;
            int top = _player.Y;
            int right = tableLayoutPanel1.ColumnCount + left;
            int bottom = tableLayoutPanel1.RowCount + top;

            for (int i = left; i < right; ++i)
            {
                for (int j = top; j < bottom; ++j)
                {
                    _panelMap[i - left, j - top].BackgroundImage = BlocSet.GetSingleton()[_map[i, j]].Image;
                    _panelMap[i - left, j - top].Tag = new Point(i, j);
                }
            }
        }
        #endregion

        #region Controles
        void Form1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if (e.KeyData == Keys.Up || e.KeyData == Keys.Down ||
                e.KeyData == Keys.Left || e.KeyData == Keys.Right)
            {
                if (e.KeyData == Keys.Up)
                {
                    int y = _player.Y - 1;
                    _player.Y = Math.Max(0, y);
                }
                if (e.KeyData == Keys.Left)
                {
                    int x = _player.X - 1;
                    _player.X = Math.Max(0, x);
                }
                if (e.KeyData == Keys.Down)
                {
                    int y = _player.Y + 1;
                    _player.Y = Math.Min(_height - tableHeight, y);
                }
                if (e.KeyData == Keys.Right)
                {
                    int x = _player.X + 1;
                    _player.X = Math.Min(_length - tableLength, x);
                }
                Build();
            }
            else if (e.KeyData == Keys.Escape)
            {
                
            }
        }

        void p_MouseClick(object sender, MouseEventArgs e)
        {
            Panel p = (Panel)sender;
            Point i = (Point)p.Tag;
            if (e.Button == MouseButtons.Right)
                _map[i] = (TypeBloc)_listBlocs[_selectedBloc];
            else if (e.Button == MouseButtons.Left)
            {
                BlocSet.GetSingleton()[_map[i]].Remove(_map, i);
            }
            Build();
        }

        void Form1_MouseWheel(object sender, MouseEventArgs e)
        {
            if (e.Delta < 0)
            {
                _selectedBloc++;
                if (_selectedBloc >= tableLayoutPanel2.ColumnCount)
                    _selectedBloc = 0;
            }
            else
            {
                _selectedBloc--;
                if (_selectedBloc < 0)
                    _selectedBloc = tableLayoutPanel2.ColumnCount - 1;
            }
            Pnl_Selected.Location = new Point(_selectedBloc * 17 + (tableLayoutPanel2.Location.X - 4), Pnl_Selected.Location.Y);
            Pnl_Selected.BackgroundImage = BlocSet.GetSingleton()[_listBlocs[_selectedBloc]].Image;
        }
        #endregion
    }
}
